window.onload = init;

function init() {

	var button = document.getElementById("add_button");
	button.onclick = createSticky;

	var send_button = document.getElementById("send_button");
	send_button.onclick = saveOnserver;
	
	var stickiesarray = getStickiesarray();

	for ( var i = 0; i < stickiesarray.length; i++) {
		var key = stickiesarray[i];
		var value = JSON.parse(localStorage[key]);
		addStickyToDOM(key , value);
	}

}

function addStickyToDOM(key, stickyObj) {
	var stickies = document.getElementById("stickies");
	var sticky = document.createElement("li");
	var span = document.createElement("span");
	span.setAttribute("class", "sticky");
	sticky.setAttribute("id", key);
	
	sticky.style.backgroundColor = stickyObj.color;
	
	span.innerHTML = stickyObj.value;
	sticky.appendChild(span);
	stickies.appendChild(sticky);
	sticky.onclick = deleteSticky;
}

function createSticky() {
	var stickiesarray = getStickiesarray();
	var currentDate = new Date();
	
	var colorSelectObj = document.getElementById("note_color");
	var index = colorSelectObj.selectedIndex;
	var color = colorSelectObj[index].value;
	
	
	var key = "sticky_" + currentDate.getTime();
	var value = document.getElementById("note_text").value;
	localStorage.setItem(key, value);
	
	var stickyObj = {
			"value": value,
			"color": color
			};
	localStorage.setItem(key, JSON.stringify(stickyObj));
	
	stickiesarray.push(key);
	localStorage.setItem("stickiesarray", JSON.stringify(stickiesarray));
	addStickyToDOM(key , stickyObj);

}

function getStickiesarray() {
	var stickiesarray = localStorage.getItem("stickiesarray");
	if (!stickiesarray) {
		stickiesarray = [];
		localStorage.setItem("stickiesarray", JSON.stringify(stickiesarray));
	} else {
		stickiesarray = JSON.parse(stickiesarray);
	}
	return stickiesarray;
}




function getStickiesarrayFromServer() {
	var stickiesarray = localStorage.getItem("stickiesarray");
	if (!stickiesarray) {
		stickiesarray = [];
		localStorage.setItem("stickiesarray", JSON.stringify(stickiesarray));
	} else {
		stickiesarray = JSON.parse(stickiesarray);
	}
	return stickiesarray;
}




function deleteSticky(e) {
	
//	var key = e.target.id ;
	
	var key = e.target.id;
		if (e.target.tagName.toLowerCase() == "span") {
			key = e.target.parentNode.id;
	}
	
	
	localStorage.removeItem(key);
	var stickiesarray = getStickiesarray();
	if (stickiesarray) {
		for ( var i = 0; i < stickiesarray.length; i++) {
			if (key == stickiesarray[i]) {
				stickiesarray.splice(i, 1);
			}
		}
		localStorage.setItem("stickiesarray", JSON.stringify(stickiesarray));
	}
	
	removeStickyFromDOM(key);
}


function removeStickyFromDOM(key) {
	var sticky = document.getElementById(key);
	sticky.parentNode.removeChild(sticky);
}

function saveOnserver(){
	alert("saveOnserver");
	var text = "stickytext";
	
	var url="http://192.168.1.105:8080/wishlistapp/WishListManagerService";
	
	var req = new XMLHttpRequest();
	req.open("POST" , url);
	req.send();
		
	//client.onreadystatechange = handler;
	//client.open("GET", "unicorn.xml");
	//client.send();
	
	
}